package com.zxc.ompz;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

public class MediaListField extends ListField implements ListFieldCallback{
	private int margin = 2;
	private int width = Display.getWidth();
	private int height = Font.getDefault().getHeight()+margin;
	
	private Vector mediaVector;
	private Media currentMedia;
	private Bitmap bmpMedia;
	
//	private Font font1 = Font.getDefault().derive(Font.ITALIC|Font.BOLD, 18);
	private Font font2 = Font.getDefault().derive(Font.PLAIN, 20);
	private Font font3 = Font.getDefault().derive(Font.ITALIC, 20);
//	private Font font4 = Font.getDefault().derive(Font.BOLD, 24);
	
	public MediaListField(Vector mediaVector){
		this.mediaVector = mediaVector;
		bmpMedia = Bitmap.getBitmapResource("media.png");
		setRowHeight(height);
		setEmptyString("Menu->Add URL", DrawStyle.HCENTER);
		setFont(font2);
		setCallback(this);
	}
	public Media getCurrentMedia(){
		return currentMedia;
	}
	public void setCurrentMedia(Media m){
		currentMedia = m;
		invalidate();
	}
	public void update(){
		invalidate();
	}
    protected void drawFocus (Graphics g, boolean on) {
    	XYRect rect = new XYRect();
        int alpha = g.getGlobalAlpha();
        g.setGlobalAlpha(160);
        getFocusRect(rect);
        drawHighlightRegion(g, HIGHLIGHT_FOCUS, on, rect.x, rect.y, rect.width, rect.height);
        g.setGlobalAlpha(alpha);
    }
	/*********************** ListFieldCallback Begin**************************/
	public void drawListRow(ListField lf, Graphics g, int index, int y, int width) {
		Media m = (Media)mediaVector.elementAt(index);
		//background
		if (index%2==0){
			g.setColor(0xE8F2FE);
		}
		else{
			g.setColor(Color.WHITE);
		}
		g.fillRect(0, y, width, height);
		//local media
		if (m.isLocal()){
			g.drawBitmap(0+margin, y+(height-16)/2, 16, 16, bmpMedia, 16, 0);
		}
		//current media
		if (currentMedia!=null && m==currentMedia){
			g.setFont(font3);
			g.setColor(Color.BLACK);
			g.drawText(m.toString(), 2*margin+16, y+height/2, DrawStyle.LEFT | DrawStyle.VCENTER,
					   width-2*margin-32);
			g.drawBitmap(width-margin-16, y+(height-16)/2, 16, 16, bmpMedia, 0, 0);
		}
		else{
			g.setFont(font2);
			g.setColor(Color.BLACK);
			g.drawText(m.toString(), 2*margin+16, y+height/2, DrawStyle.LEFT | DrawStyle.VCENTER, 
					   width-2*margin-16);
		}
	}
	public Object get(ListField listField, int index) {
		return mediaVector.elementAt(index);
	}
	public int getPreferredWidth(ListField listField) {
		return width;
	}
	public int indexOfList(ListField listField, String prefix, int start) {
		return 0;
	}
	/*********************** ListFieldCallback End**************************/
}
